
package com.updesignsz.api.controller;

import com.updesignsz.api.service.ProjectsService;
import com.updesignsz.api.util.ParamsUtil;
import com.updesignsz.api.util.ResponseUtil;
import com.updesignsz.commom.util.PageRequest;
import com.updesignsz.commom.util.ResultVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 项目
 * Creation date 2020-8-8 15:07:53
 */
@RestController
@RequestMapping(value = "/api/projects", method = RequestMethod.POST)
public class ProjectsController {

	@Autowired
	private ProjectsService projectsService;

    /**
     * 项目列表
     */
    @RequestMapping(value = "/list")
    public void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ResultVo vo = new ResultVo();
        try {
            JSONObject json = ResponseUtil.getRequestAsJSONObject(request);
			PageRequest page = new PageRequest();
			page.setPageNumber(json.optInt("pageNumber", ParamsUtil.pageNumber));
			page.setPageSize(json.optInt("pageSize", ParamsUtil.pageSize));
			page.setFilters(json);
            vo.setData(projectsService.findByPage(page).getList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }

    /**
     * 项目详情
     */
    @RequestMapping("/detail")
    public void detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ResultVo vo = new ResultVo();
        try {
            JSONObject json = ResponseUtil.getRequestAsJSONObject(request);
            vo.setData(projectsService.getFrontOrAfterID(json));
        } catch (Exception e) {
            vo.setResultVo(-1, "查询新闻详情失败，系统异常！");
            e.printStackTrace();
        }
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }
}